package rpc.utils;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * @ClassName: PropertiesFileUtil
 * @Description:
 * @author: Kowk
 * @since:1.0.0
 */
@Slf4j
@NoArgsConstructor
public class PropertiesFileUtil {

    public static Properties readPropertiesFile(String fileName){
        URL url = Thread.currentThread().getContextClassLoader().getResource("");
        String rpcConfigPath = "";
        if (url != null) {
            rpcConfigPath = url.getPath() + fileName;
        }
        Properties properties = null;
        try (InputStreamReader inputStreamReader = new InputStreamReader(
                new FileInputStream(rpcConfigPath), StandardCharsets.UTF_8
        )){
            properties = new Properties();
            properties.load(inputStreamReader);
        } catch (FileNotFoundException e) {
            log.error("没有找到此配置文件[{}]",fileName);
        } catch (IOException e) {
            log.error("在读取配置时发生IO错误",fileName);
        }
        return properties;
    }

}
